import * as fs from 'fs';
import * as path from 'path';
import { ConfigModule } from '@nestjs/config';

export class AppConfigModule {
  static forRoot() {
    return ConfigModule.forRoot({
      isGlobal: true,
      cache: true,
      load: [AppConfigModule.loader],
    });
  }
  private static loader() {
    const env =
      process.env.NODE_ENV === 'production' ? 'production' : 'development';
    const configPath = path.join(process.cwd(), `config/${env}.json`);
    return JSON.parse(fs.readFileSync(configPath, { encoding: 'utf-8' }));
  }
}
